banner

SNAP - 04: BOOLEAN and VARIABLES

OPENING QUESTIONS: What do we mean by the term "Boolean?" Why is it essential to computer programming?

 

OBJECTIVE:   I will be able to write simple boolean code during today's class.

WORD FOR TODAY:

  • Boolean (true or false, yes or no, on or off, 1 or 0)
  • Mod opearator (The mod or 'modulus' operator returns the remainder when one number is divided by another. For example: 5 mod 2 evaluates 5/2 which equals 2 with a remainder of 1. Therefor the output of that expression is the remainder which is 1

WORK O' THE DAY

Remember, computers are nothing more than high speed idiots.

As such, computers work best when processing information that is either true (1) or false (0)

When writing computer code, it is frequently helpful to think like and program for, a computer.

As such, we have 3 different 'boolean operators' that we need to get a handle on:

AND, OR, NOT

In order for an AND statement to be true, each part of the operation in question MUST be true.

Let's take a look at a fun sorta weather analogy to talk about BOOLEAN operators. It might be raining outside, it might also be windy outside and it might also be windy AND rainy outside.

Is it raining outside AND is it windy outside gives us two items to compare:

      1. (Is it raining outside)
      2. (is it windy outside)

Let's break that down. Let's say we look out the window and we determine that yes, it is raining but no, it is not windy. That gives us the following data:

(Is it raining outside) = True

(is it windy outside) = False

Now let's evaluate those together using the AND Statement:

      (True) AND (False)

Since those two statements are linked by an AND statement, the computer evaluates both to see if both statements are True. If they are, the computer will return a True. Any other situation returns False

Therefore:

(True) AND (False) = False <since both statements are not true>

Soooooo, the takeway here is that ALL statements connected by an AND must be true in order for the computer to evaluate that as true. ANY presence of false will return False.

USING AND

True
and True
= True
True
and False
= False
False
and True
= False
False
and False
= False

 

The OR operator is a bit easier to get used to.

The OR operator basically checks to see if EITHER expression is true. If either expression is true the command returns true:

True
or True
= True
True
or False
= True
False
or True
= True
False
or False
= False

═══════════════════

The NOT operator

The NOT operator can be a bit tricky at times. The NOT operator checks to see if the reverse of <whatever the statement is> is true.

If the reverse of the <whatever statement> is true, the NOT operator returns true:

For example let's take a look at our rain question again:

Is it (NOT) (raining outside today)

If (raining outside today) = true then let's subsitute True in for our statement that we are evaluating:

(NOT) (TRUE)

and we all know that not true = FALSE

so in that case:

Is it (NOT) (raining outside today) = FALSE

However, let's re-evaluate for the case where it is NOT raining outside:

Let's start with our initial statement

Is it (NOT) (raining outside today)

it isn't raining outside today so (raining outside today) = False

NOT (False)

and NOT False = True!

Whooo hooooo!

Let's take a look at Lab 2.5

 

 

═══════════════════════════

HOMEWORK: